package com.my.house.common;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Optional;

/**
 * 异常基类
 *
 * @author Oct
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BizException extends RuntimeException {

    private final Integer code;

    /**
     * 示例调用
     * Assert.isTrue(Objects.nonNull(companyId), AttendException::param);
     */
    public static BizException param() {
        return new BizException("参数错误");
    }

    /**
     * 示例调用
     * Assert.isTrue(Objects.nonNull(companyId), AttendException::param);
     */
    public static BizException system() {
        return new BizException("系统异常，请联系管理员");
    }

    public BizException() {
        super();
        this.code = ApiResult.ERROR;
    }

    public BizException(String message) {
        super(message);
        this.code = ApiResult.ERROR;
    }

    public BizException(Integer code, String message) {
        super(message);
        this.code = Optional.ofNullable(code).orElse(ApiResult.ERROR);
    }

    @Override
    public String toString() {
        return "code: " + code + ", message: " + this.getMessage();
    }

}
